package com.zsn.ai.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.zsn.ai.entity.po.ChatMessageHistory;
import com.zsn.ai.entity.po.ChatidHistory;
import com.zsn.ai.service.IChatidHistoryService;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @program: ai-demo
 * @ClassName InmemoryChatHistoryRepository
 * @description:
 * @author: zsn
 * @create: 2025-09-24 01:05
 * @Version 1.0
 **/
@RequiredArgsConstructor
@Component
public class InmemoryChatHistoryRepository implements ChatHistoryRepository{
    private final Map<String, List<String>> chatHistory = new HashMap<>();
    private static final String CHAT_IDS_KEY = "chat:chat-ids:";

    private final RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private IChatidHistoryService chatidHistoryService;
    @Override
    public void save(String type, String chatId) {
//        List<String> chatIds = chatHistory.computeIfAbsent(type, k -> new ArrayList<>());
//        if (chatIds.contains(chatId)){
//            return;
//        }
//        chatIds.add(chatId);
//        List<Object> objects = redisTemplate.opsForList().range(CHAT_IDS_KEY + type, 0, -1);
//        if (CollectionUtils.isEmpty(objects) || !objects.contains(chatId)){
//            redisTemplate.opsForList().leftPush(CHAT_IDS_KEY + type, chatId);
        LambdaQueryWrapper<ChatidHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatidHistory::getType, type)
                .eq(ChatidHistory::getChatId, chatId);
        List<ChatidHistory> list = chatidHistoryService.list(wrapper);
        if (!CollectionUtils.isEmpty(list)){
            return;
        }
        ChatidHistory chatidHistory = new ChatidHistory();
        chatidHistory.setChatId(chatId);
        chatidHistory.setType(type);
        chatidHistory.setAddTime(LocalDate.now());
        chatidHistoryService.save(chatidHistory);
    }

    @Override
    public List<String> getChatIds(String type) {
//        List<String> chatIds = chatHistory.get(type);
//        return chatIds == null ? new ArrayList<>() : chatIds;
//        return chatHistory.getOrDefault(type, new ArrayList<>());
//        List<Object> objects = redisTemplate.opsForList().range(CHAT_IDS_KEY + type, 0, -1);
//        return objects == null ? new ArrayList<>() : objects.stream().map(Object::toString).toList();
        LambdaQueryWrapper<ChatidHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatidHistory::getType, type);
        List<String> chatIds = chatidHistoryService.list(wrapper).stream().map(ChatidHistory::getChatId).toList();
        return chatIds == null ? new ArrayList<>() : chatIds;
    }
}